﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sipfe.Controllers.Base;
using sipfe.service.autoayuda;

using sipfe.model.dataAccess;
using sipfe.service.mante;
using sipfe.util;



namespace sipfe.Controllers.Mante
{
    public class CompendioManteController : BaseSipfeController
    {
        string Titulo = "MANTENIMIENTO DE LOS COMPENDIO DE DATOS MAESTROS DEL SISTEMA";

        public ActionResult CompendioCab()
        {
            ViewBag.TITULO = Titulo;
            return View();
        }

        public ViewResult CompendioDet()
        {
            string sw               =   Request["sw"];
            string nameTable        =   Request["nameTable"];
            string idTable          =   Request["idTable"];
            string strNumRegShow    =   Request["numRegShow"];

            CompendioManteService compendioService = new CompendioManteService();

            IList<dynamic> lista = compendioService.listaTablaCompendio(sw, nameTable.ToUpper(), idTable);
            int NumTotalRegistros = lista.Count();

            if (NumTotalRegistros > 0)
            {
                setPaginacion(lista, strNumRegShow, NumTotalRegistros);
                return View(lista);
            }
            return null;
        }

        public ViewResult AgregarTablaInp()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AgregarTablaInp(SIPMaeCompendio sipMaeCompendio)
        {
            if (ModelState.IsValid)
            {
                CompendioManteService compendioManteService = new CompendioManteService();
                String[] rpta = compendioManteService.AgregarTablaInpService(sipMaeCompendio);
                return RedirectToAction("RptaAddTabla", new { idTabla = rpta[0], nomTabla = rpta[1] });
            }
            return View(sipMaeCompendio);
        }

        public ViewResult RptaAddTabla(string idTabla, string nomTabla)
        {
            ViewBag.ID_TABLA    =   idTabla;
            ViewBag.NOM_TABLA   =   nomTabla;
            return View();
        }

        public ViewResult compendioDetalleDet()
        {
            int idCompendio = Convert.ToInt32(Request["idCompendio"]);
            CompendioManteService compendioManteService = new CompendioManteService();
            return View(compendioManteService.compendioDetalleDetService(idCompendio));
        }

        public ActionResult CompendioElementoDet(decimal nIdeDetalle)
        {
           CompendioManteService compendioManteService = new CompendioManteService();
            return View(compendioManteService.sipDetCompendioDetalleByIdService(nIdeDetalle));
        }

        public ViewResult AgregarCompendioDetalle(decimal nIdeCompendio,string idTD)
        {
            ViewBag.CAMPO = idTD;
            return View();
        }

        [HttpPost]
        public ActionResult AgregarCompendioDetalle(SIPDetCompendio sipDetCompendio)
        {
            if (ModelState.IsValid)
            {
                string idTD = Request["idTD"];
                CompendioManteService compendioManteService = new CompendioManteService();
                compendioManteService.AgregarCompendioDetalleService(sipDetCompendio);
                return RedirectToAction("RptaAddCompDet", new { idTD = idTD });
            }
            return View(sipDetCompendio);
        }

        public ViewResult RptaAddCompDet(string idTD)
        {
            ViewBag.CAMPO = idTD;
            return View();
        }

        public ViewResult UpdateCompendio(decimal nIdeCompendio)
        {
          CompendioManteService compendioManteService = new CompendioManteService();
          return View(compendioManteService.sipMaeCompendioByIdService(nIdeCompendio));
        }

        [HttpPost]
        public ActionResult UpdateCompendio(SIPMaeCompendio sipMaeCompendio)
        {
            if (ModelState.IsValid)
            {
                CompendioManteService compendioManteService = new CompendioManteService();                
                String[] rpta = compendioManteService.updateMaeCompenedioService(sipMaeCompendio);
                return RedirectToAction("RptaAddTabla", new { idTabla = rpta[0], nomTabla = rpta[1] });
            }
            return View(sipMaeCompendio);
        }

        public ViewResult UpdateCompendioDetalle(decimal nIdeDetalle)
        {
            ViewBag.CAMPO = Request["idTD"];
            CompendioManteService compendioManteService = new CompendioManteService();
            return View(compendioManteService.sipDetCompendioDetalleByIdService(nIdeDetalle));
        }

        [HttpPost]
        public ActionResult UpdateCompendioDetalle(SIPDetCompendio sipDetCompendio)
        {
            if (ModelState.IsValid)
            {
                string idTD = Request["idTD"];
                CompendioManteService compendioManteService = new CompendioManteService();
                String[] rpta = compendioManteService.updateDetCompenedioService(sipDetCompendio);
                return RedirectToAction("RptaAddCompDet", new { idTD = idTD });
            }
            return View(sipDetCompendio);
        }

        public HtmlString cerrarVigenciaCompDetalle(decimal nIdeDetalle)
        {
            CompendioManteService compendioManteService = new CompendioManteService();            
            DateTime hoy = UtilitariosUtil.FechaHoy();
            int rpta = compendioManteService.cerrarVigenciaCompDetalleService(nIdeDetalle, hoy);
            return new HtmlString((rpta == 1) ? hoy.ToString("dd/MM/yyyy") : "");
        }

        public ViewResult ListaElementosDelGrupo()
        {
            int nIdeDetalleGrupo = Convert.ToInt32(Request["nIdeDetalleGrupo"]);
            CompendioManteService compendioManteService = new CompendioManteService();
            return View(compendioManteService.listaElementosDelGrupoService(nIdeDetalleGrupo));            
        }

        public ViewResult AgregarElementoAlGrupo()
        {
            return View();
        }

        public ViewResult ListaElemenForGrup()
        {
            string idTable = Convert.ToString(Request["idTable"]);
            string nIdeDetalleAux = Convert.ToString(Request["nIdeDetalleAux"]);
            CompendioManteService compendioManteService = new CompendioManteService();
            IList<dynamic> lista = compendioManteService.ListaElemenForGrupService(idTable, nIdeDetalleAux);            
            return View(lista);
        }

        public HtmlString addItemAlGrupo()
        {
            CompendioManteService compendioManteService = new CompendioManteService();
            SIPDetCompGrupo sipDetCompGrupo = new SIPDetCompGrupo();

            int nIdeCompendio = Convert.ToInt32(Request["nIdeCompendio"]);
            int nIdeDetalle = Convert.ToInt32(Request["nIdeDetalle"]);

            sipDetCompGrupo.dtFecIniVig = DateTime.Now;
            sipDetCompGrupo.dtFecReg = DateTime.Now;
            sipDetCompGrupo.nEstado = Constante.ESTADO_REGISTRO;
            sipDetCompGrupo.nIdeDetalleGrupo = nIdeCompendio;
            sipDetCompGrupo.nIdeDetElemento = nIdeDetalle;
            sipDetCompGrupo.nIdeUsuReg = Convert.ToInt32(Session[Constante.N_IDE_USUARIO]);
            //string cantidad = cs.addItemAlGrupoService(sipDetCompGrupo);
            return new HtmlString(compendioManteService.addItemAlGrupoService(sipDetCompGrupo));
        }

        public HtmlString cerrarVigenciaEnGrupo()
        {
            CompendioManteService compendioManteService = new CompendioManteService();
            int nIdeGrupo = Convert.ToInt32(Request["nIdeGrupo"]);
            DateTime hoy = UtilitariosUtil.FechaHoy();
            int rpta = compendioManteService.cerrarVigenciaEnGrupoService(nIdeGrupo, hoy);
            return new HtmlString((rpta == 1) ? hoy.ToString("dd/MM/yyyy") : "");
        }

    }
}
